<?php
/**
 *@author Connor <caokang@foxmail.com>
 *@todo
 *@link www.zeevin.com
 */
class SpaceController extends CController{
	public $sp_uid;
	public $space_privacy;//个人空间隐私设置
	public function init(){
		parent::init();
		$this->sp_uid = $_GET['id']?intval($_GET['id']):(Yii::app()->user->isGuest?0:Yii::app()->user->id);
		$this->space_privacy = Yii::app()->db->createCommand()
								->select('space_privacy')
								->from('users')
								->where('id=?',array($this->sp_uid))
								->queryScalar();
		$redirect=false;

		switch ($this->space_privacy) {
								case User::SPACE_PRIVACY_PUBLIC:
									# code...
									break;
								case User::SPACE_PRIVACY_LOGIN:
									if(Yii::app()->user->isGuest)$redirect=true;
									break;
								case User::SPACE_PRIVACY_FRIEND:
									if(Yii::app()->user->isGuest)$redirect=true;
									else
									if(!Follower::isFollow(Yii::app()->user->id,$this->sp_uid)&&$this->sp_uid!=Yii::app()->user->id)$redirect=true;
									break;
								default:
									# code...
									break;
							}
		// 					var_dump($this->sp_uid);
		// var_dump($redirect);
		if($redirect)$this->redirect('?r=home');					
		
	}
	public function actionIndex(){
		$split = 'zichash';
		$drop_split='drop_del';
		if(!User::model()->countByAttributes(array('id'=>$this->sp_uid)))
			$this->redirect(array('site/index'));

		$space_user = User::model()->findByPk($this->sp_uid);
		$this->pageTitle=$space_user->nickname.'的空间_之嗅交友社区';
		$tags = Yii::app()->db->createCommand()
				->select('a.id,a.tag_id,b.tag_name')
				->from('user_tags a')
				->leftJoin('tags b','a.tag_id=b.id')
				->where('a.uid=? and a.type=?',array($this->sp_uid,0))
				->query();

		$profile = UserIntro::model()->findByAttributes(array('uid'=>$this->sp_uid));

		$pics = Pic::model()->findAllByAttributes(array('uid'=>$this->sp_uid));

		$posts = Yii::app()->db->createCommand()
								->select('id,subject,content,ctime')
								->from('user_blogs')
								->where('uid=?',array($this->sp_uid))
								->order('id DESC')
								->limit(3)
								->queryAll();

		$intro = Profile::model()->findByAttributes(array('uid'=>$this->sp_uid));

		$follow = Follower::getFollowStatus($this->sp_uid);
		$tend = UserSetting::model()->findByPk($this->sp_uid);
		$feed = Yii::app()->db->createCommand()
								->select('id,type,uid,link,date')
								->from('feed')
								->where('uid=?',array($this->sp_uid))
								->order('id DESC')
								->limit(15)
								->queryAll();
		// var_dump($follow);
		$this->render('index',array('user'=>$space_user,'tags'=>$tags,'profile'=>$profile,
									'pics'=>$pics,'posts'=>$posts,'intro'=>$intro,'follow'=>$follow,'tend'=>$tend,'feed'=>$feed,'split'=>$split,'drop_split'=>$drop_split));
	}

	public function actionPic(){
		$pics = Pic::model()->findAllByAttributes(array('uid'=>$this->sp_uid));
		$this->renderPartial('pic',array('pics'=>$pics));
	}
	//博客列表弹出页
	public function actionBlog(){
		$posts = Post::getList($this->sp_uid);
		if(Yii::app()->request->isAjaxRequest){	
			foreach($posts['rows'] as $v){
				$html.='<li><h3>'.CHtml::link(CHtml::encode(mb_substr($v->subject,0,37)),array('post/edit','id'=>$v->id),array('title'=>CHtml::encode($v->subject))).' '.substr($v->ctime,0,10).'</h3><p>'.CHtml::encode($v->sub_content).'......</p></li>';
			}
		}
		
		$this->renderPartial('post',array('posts'=>$posts));
	}

	//博客详情弹出页
	public function actionBlogView(){
		$rs = Post::model ()->findbyPk($_GET ['id']);
		$this->renderPartial('bviews',array('rs'=>$rs));
	}

	public function actionIntro(){
		$split = 'zichash';
		$drop_split='drop_del';
		$profile = Profile::model()->findByAttributes(array('uid'=>$this->sp_uid));
		$hook = ProfileHook::model()->findAllByAttributes(array('uid'=>$this->sp_uid));
		$this->renderPartial('intro',array('profile'=>$profile,'hook'=>$hook,'split'=>$split,'drop_split'=>$drop_split));
	}

	public function actionFollow(){
		if(Yii::app()->request->isAjaxRequest){
			$fid = intval($_POST['id']);

			if(Yii::app()->user->isGuest){
				$msg['status']=1;
				$msg['desc']='请登陆后再操作';
			}elseif(!empty($fid)){
				if($fid==Yii::app()->user->id){
					$msg['status']=1;
					$msg['desc']='你不能关注自己';
				}else
				$msg = Follower::follow($fid);
				
			}
			echo json_encode($msg);
		}
	}
}